package game.module.mj.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRound;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMjForceDismisroom;
import lua.LuaMessageMj.PushMJForceDismissRoom;
import lua.LuaMessageMj.S2CMjForceDismisroom;

@MsgCodeAnn(msgcode = C2SMjForceDismisroom.id, accessLimit = 500)
public class MjFoceDismissRoom extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(MjFoceDismissRoom.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("mj force to dismiss room,playerId={}", playerId);
		// 房间是否存在
		// 房间是否存在
		MjRound mjround = MjDataManager.getInstance().getMjRoom(playerId);
		if (mjround == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		S2CMjForceDismisroom msg = new S2CMjForceDismisroom();
		playingRole.writeAndFlush(msg.build(playingRole.alloc()));
		List<Integer> players = mjround.getPlayerIds();
		PushMJForceDismissRoom pushmsg = new PushMJForceDismissRoom();
		pushmsg.player_index = (byte) players.indexOf(playerId);
		for(Integer aplayer:players) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aplayer);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
				// 推送关于出牌的信息
			}
		}
		MjLogic.getInstance().testRoundOver(mjround);
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

}
